﻿using UnityEngine;
using System.Collections;

public class GfxFieldTextureMulti : MonoBehaviour {
	
	public WorldField[] fields;
	public Color[] colors;

	public Texture2D tex;
	public MeshRenderer rendererr = null;
	public int res = 32;
		
	void Start () {

		if (rendererr == null) rendererr = gameObject.GetComponent<MeshRenderer> ();
		tex = new Texture2D (res, res, TextureFormat.ARGB32, false);
		for (int x = 0; x < res; x++) {
			for (int y = 0; y < res; y++) {
				tex.SetPixel (x, y, Color.gray);
			}
		}
		tex.Apply ();
		rendererr.material.mainTexture = tex;
	}
	
	
	void Update () {
		for (int x = 0; x < res; x++) {
			for (int y = 0; y < res; y++) {
				float f = 1;
				Color c = Color.black;
				float fx = ((x+0.5f)/res)-0.5f;
				float fy = ((y+0.5f)/res)-0.5f;
				Vector3 local = new Vector3(-fx, 0, -fy);
				for (int i = 0; i < fields.Length; i++) {
					f = fields[i].getIntensity((Vector3)(transform.localToWorldMatrix * local) + 0*transform.position);
					//f = (x % 2) * (y % 2);
					c  = c + colors[i] * f;
				}
				//Color c = Color.white * f;
				tex.SetPixel (x, y, c);
			}
		}
		tex.Apply ();
		rendererr.material.mainTexture = tex;
		rendererr.material.color = Color.white;
	}
}
